﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Common.Tools
{
    public class DESEncryptor
    {
        #region 私有成员
        /// <summary> 
        /// 输入字符串 
        /// </summary> 
        private string _inputString;
        /// <summary> 
        /// 输出字符串 
        /// </summary> 
        private string _outString;
        /// <summary> 
        /// 输入文件路径 
        /// </summary> 
        private string _inputFilePath;
        /// <summary> 
        /// 输出文件路径 
        /// </summary> 
        private string _outFilePath;
        /// <summary> 
        /// 加密密钥 
        /// </summary> 
        private string _encryptKey;
        /// <summary> 
        /// 解密密钥 
        /// </summary> 
        private string _decryptKey;
        /// <summary> 
        /// 提示信息 
        /// </summary> 
        private string _noteMessage;
        #endregion

        #region 公共属性
        /// <summary> 
        /// 输入字符串 
        /// </summary> 
        public string InputString
        {
            get { return _inputString; }
            set { _inputString = value; }
        }
        /// <summary> 
        /// 输出字符串 
        /// </summary> 
        public string OutString
        {
            get { return _outString; }
            set { _outString = value; }
        }
        /// <summary> 
        /// 输入文件路径 
        /// </summary> 
        public string InputFilePath
        {
            get { return _inputFilePath; }
            set { _inputFilePath = value; }
        }
        /// <summary> 
        /// 输出文件路径 
        /// </summary> 
        public string OutFilePath
        {
            get { return _outFilePath; }
            set { _outFilePath = value; }
        }
        /// <summary> 
        /// 加密密钥 
        /// </summary> 
        public string EncryptKey
        {
            get { return _encryptKey; }
            set { _encryptKey = value; }
        }
        /// <summary> 
        /// 解密密钥 
        /// </summary> 
        public string DecryptKey
        {
            get { return _decryptKey; }
            set { _decryptKey = value; }
        }
        /// <summary> 
        /// 错误信息 
        /// </summary> 
        public string NoteMessage
        {
            get { return _noteMessage; }
            set { _noteMessage = value; }
        }
        #endregion

        #region 构造函数
        public DESEncryptor()
        {
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        }
        #endregion

        #region DES加密字符串
        /// <summary> 
        /// 加密字符串 
        /// 注意:密钥必须为８位 
        /// </summary> 
        public bool DesEncrypt()
        {
            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(DESConfig.RgbKey.Substring(0, 8));
                var des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(this._inputString);
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateEncryptor(byKey, DESConfig.RgbIV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                this._outString = Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception error)
            {
                return false;
            }
            return true;
        }

        public string DesEncrypt(string content)
        {
            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(DESConfig.RgbKey.Substring(0, 8));
                var des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(content);
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateEncryptor(byKey, DESConfig.RgbIV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region DES解密字符串
        /// <summary> 
        /// 解密字符串 
        /// </summary> 
        public bool DesDecrypt()
        {
            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(DESConfig.RgbKey.Substring(0, 8));
                var des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Convert.FromBase64String(this._inputString);
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateDecryptor(byKey, DESConfig.RgbIV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                Encoding encoding = new UTF8Encoding();
                this._outString = encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public string DesDecrypt(string content)
        {
            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(DESConfig.RgbKey.Substring(0, 8));
                var des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Convert.FromBase64String(content);
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateDecryptor(byKey, DESConfig.RgbIV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                Encoding encoding = new UTF8Encoding();
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception error)
            {
                return null;
            }
        }
        #endregion

        #region DES加密文件
        /// <summary> 
        /// DES加密文件 
        /// </summary> 
        public bool FileDesEncrypt()
        {
            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(DESConfig.RgbKey.Substring(0, 8));
                var fin = new FileStream(this._inputFilePath, FileMode.Open, FileAccess.Read);
                var fout = new FileStream(this._outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
                fout.SetLength(0);
                //Create variables to help with read and write. 
                var bin = new byte[100]; //This is intermediate storage for the encryption. 
                long rdlen = 0; //This is the total number of bytes written. 
                long totlen = fin.Length; //This is the total length of the input file. 
                DES des = new DESCryptoServiceProvider();
                var encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, DESConfig.RgbIV), CryptoStreamMode.Write);



                //Read from the input file, then encrypt and write to the output file. 
                while (rdlen < totlen)
                {
                    int len = fin.Read(bin, 0, 100); //This is the number of bytes to be written at a time. 
                    encStream.Write(bin, 0, len);
                    rdlen = rdlen + len;
                }
                encStream.Close();
                fout.Close();
                fin.Close();


            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        #endregion


        #region DES解密文件
        /// <summary> 
        /// 解密文件 
        /// </summary> 
        public bool FileDesDecrypt()
        {
            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(DESConfig.RgbKey.Substring(0, 8));
                var fin = new FileStream(this._inputFilePath, FileMode.Open, FileAccess.Read);
                var fout = new FileStream(this._outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
                fout.SetLength(0);
                //Create variables to help with read and write. 
                var bin = new byte[100]; //This is intermediate storage for the encryption. 
                long rdlen = 0; //This is the total number of bytes written.  
                long totlen = fin.Length; //This is the total length of the input file. 
                DES des = new DESCryptoServiceProvider();
                var encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, DESConfig.RgbIV), CryptoStreamMode.Write);


                //Read from the input file, then encrypt and write to the output file. 
                while (rdlen < totlen)
                {
                    int len = fin.Read(bin, 0, 100); //This is the number of bytes to be written at a time. 
                    encStream.Write(bin, 0, len);
                    rdlen = rdlen + len;
                }
                encStream.Close();
                fout.Close();
                fin.Close();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        #endregion

  
    }

    public class DESConfig
    {
        private static string _rgbKey = "20130701";
        private static byte[] _rgbIV = new byte[] { 136, 7, 25, 188, 4, 104, 254, 93 };

        public static string RgbKey
        {
            get
            {
                return "20130701";
            }
        }
        public static byte[] RgbIV
        {
            get
            {
                return new byte[] { 136, 7, 25, 188, 4, 104, 254, 93 };
            }
        }
    }
}
